package com.yanqu.road.server.manager.tradewar;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eTradeWarBattleType;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.entity.tradewar.*;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroup;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupUnion;
import com.yanqu.road.logic.bussiness.tradewar.TradeWarActivityBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.cross.CrossTradeWarWantedRankListModel;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.logic.pb.CrossTradeWarPb;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.logic.pb.TradeWarPb;
import com.yanqu.road.logic.sort.TradeWarWantedSort;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.cross.tradewar.CrossTradeWarProto;
import com.yanqu.road.pb.rank.RankProto;
import com.yanqu.road.pb.tradewar.TradeWarProto;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.config.GoodsMgr;
import com.yanqu.road.server.manager.union.activitygroup.Cross2UnionActivityGroupMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.LocalDateTimeHelper;
import com.yanqu.road.utils.UUIDHelper;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

public class CrossTradeWarActivity {

    /**
     * 活动信息
     */
    ActivityInfo activityInfo;

    // 分组相关
    private int groupId;
    private CrossTradeWarActivityGroup group;
    private UnionActivityGroup groupData ;

    private final Logger logger = LogManager.getLogger(CrossTradeWarActivity.class);

    //全部换成分组的数据
    private long lastNoticeResetTime;
    private CrossTradeWarWantedRankListModel wantedRankMap;       //通缉排行榜

    private LinkedList<CrossTradeWarNotice> crossTradeWarNoticeMap;                               //跨服商战公告
    private TradeWarWantedSort tradeWarWantedSort = new TradeWarWantedSort();
    private Map<String, TradeWarWanted> crossTradeWarWantedMap = new ConcurrentHashMap<>();          //通缉id 所有通缉
    private LinkedList<TradeWarWanted> finishTradeWarWantedMap = new LinkedList<>();                   //通缉id 已完成通缉
    private TradeWarProto.TradeWarWantedListMsg.Builder tradeWarWantedListMsg;                        // 通缉list msg
    private Map<String, List<TradeWarTrackRecord>> tradeWarTrackRecordMap = new ConcurrentHashMap<>();           //uid 通缉记录
    private CrossTradeWarProto.CrossTradeWarNoticeListMsg.Builder crossTradeWarNoticeMsg;       //分组 跨服商战公告
    private Map<String, Map<String, List<CrossTradeWarSimpleNotice>>> crossTradeWarSimpleNoticeMap;    //商战公告简略信息

    private Map<String, Map<String, CrossTradeWarWantedRecord>> wantedRecordMap;

    public CrossTradeWarActivity(ActivityInfo activityInfo, CrossTradeWarActivityGroup tmpGroup, int groupId,UnionActivityGroup groupData) {
        this.activityInfo = activityInfo;
        this.group = tmpGroup;
        this.groupId = groupId;
        this.groupData = groupData;
    }

    public boolean initData(LinkedList<CrossTradeWarNotice> noticeListFromDb, Map<String, TradeWarWanted> crossTradeWarWantedMapFromDb, Map<String, List<TradeWarTrackRecord>> tradeWarTrackRecordMapFromDb) {
        //分组
        crossTradeWarNoticeMsg = CrossTradeWarProto.CrossTradeWarNoticeListMsg.newBuilder();
        initTradeWarNoticeList(noticeListFromDb);

        initNoticeMsg();

        Map<String, TradeWarWanted>  tempTradeWarWantedMap = new ConcurrentHashMap<>();
        for (Map.Entry<String, TradeWarWanted> entry :crossTradeWarWantedMapFromDb.entrySet()){
            if (Cross2UnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(), entry.getValue().getUserId()) != groupId) {
                continue;
            }
            tempTradeWarWantedMap.put(entry.getKey(),entry.getValue());
        }
        crossTradeWarWantedMap = tempTradeWarWantedMap;

        wantedRankMap = initWantedRank();
        wantedRecordMap = initWantedRecordMap();
        finishTradeWarWantedMap = new LinkedList<>();

        initTradeWarWantedMsg();

        for (Map.Entry<String, List<TradeWarTrackRecord>> entry:tradeWarTrackRecordMapFromDb.entrySet()){
            boolean isThisGroup = true;
            for (TradeWarTrackRecord tradeWarTrackRecord:entry.getValue()){
                if (Cross2UnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(), tradeWarTrackRecord.getUserId()) != groupId){
                    isThisGroup = false;
                    break;
                }
            }
            if (isThisGroup){
                tradeWarTrackRecordMap.put(entry.getKey(),entry.getValue());
            }
        }
        return true;

    }

    public void save() {
        List<CrossTradeWarNotice> tempList;
        synchronized (crossTradeWarNoticeMap) {
            tempList = new LinkedList<>(crossTradeWarNoticeMap);
        }
        for (CrossTradeWarNotice data : tempList) {
            if (data.isInsertOption()) {
                TradeWarActivityBussiness.addCrossTradeWarNotice(data);
            }
        }

        List<TradeWarWanted> tempWantedList = new ArrayList<>(crossTradeWarWantedMap.values());
        for (TradeWarWanted tradeWarWanted : tempWantedList) {
            if (tradeWarWanted.isInsertOption()) {
                TradeWarActivityBussiness.addTradeWarWanted(tradeWarWanted);
            } else if (tradeWarWanted.isUpdateOption()) {
                TradeWarActivityBussiness.updateTradeWarWanted(tradeWarWanted);
            }
        }


        List<TradeWarWanted> tempFinishWantedList = new ArrayList<>(finishTradeWarWantedMap);
        for (TradeWarWanted tradeWarWanted : tempFinishWantedList) {
            if (tradeWarWanted.isInsertOption()) {
                TradeWarActivityBussiness.addTradeWarWanted(tradeWarWanted);
            } else if (tradeWarWanted.isUpdateOption()) {
                TradeWarActivityBussiness.updateTradeWarWanted(tradeWarWanted);
            }
        }

        for (List<TradeWarTrackRecord> dataList : tradeWarTrackRecordMap.values()) {
            List<TradeWarTrackRecord> tempTrackList = new ArrayList<>(dataList);
            for (TradeWarTrackRecord trackRecord : tempTrackList) {
                if (trackRecord.isInsertOption()) {
                    TradeWarActivityBussiness.addTradeWarTrackRecord(trackRecord);
                } else if (trackRecord.isUpdateOption()) {
                    TradeWarActivityBussiness.updateTradeWarTrackRecord(trackRecord);
                }
            }
        }
        return;
    }

    public void resetOneDay() {
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp();
        if (lastNoticeResetTime < zeroTime) {
            synchronized (crossTradeWarNoticeMap) {
                crossTradeWarNoticeMap.clear();
            }
            lastNoticeResetTime = zeroTime;
            //构建公告
            initNoticeMsg();
            //分发到各个游戏服
            sendServerResetMsg();
        }
    }

    /**
     * 发送重置公告到游戏服
     */
    private void sendServerResetMsg() {
        Map<Integer, ActivityInfo> activityInfoMap = CrossTradeWarActivityMgr.getOpenActivityMap();
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            notifyServerNoticeList(activityInfo.getActivityId());
        }
    }

    private void initNoticeMsg() {
        synchronized (crossTradeWarNoticeMsg) {
            CrossTradeWarProto.CrossTradeWarNoticeListMsg.Builder listMsg;
            if (crossTradeWarNoticeMap.size() > 0) {
                listMsg = parseCrossTradeWarNoticeListMsg(crossTradeWarNoticeMap);
            } else {
                listMsg = CrossTradeWarProto.CrossTradeWarNoticeListMsg.newBuilder();
            }
            crossTradeWarNoticeMsg = listMsg;
        }
    }

    /**
     * 根据战斗结果增加公告
     *
     * @param msg
     */
    public void addNoticeByResult(CrossTradeWarProto.CrossTradeWarToEnemyBattleRespMsg msg) {

        int serverCount = 1;
        if (null != groupData) {
            serverCount = groupData.getServerSet().size();
        }
        if (msg.getKillPatronsCount() < ConfigMgr.getCrossTradeWarNoticeNeedKillPatronsCount(serverCount)) {//没超过击杀最低限制，不加公告
            return;
        }
        CrossTradeWarNotice data = new CrossTradeWarNotice();
        data.setActivityId(msg.getActivityId());
        data.setUserId(msg.getMyUserId());
        data.setEnemyUserId(msg.getEnemyUserId());
        data.setBattleType(msg.getBattleType());
        data.setKillPatronsCount(msg.getKillPatronsCount());
        data.setAllKill(msg.getKillPatronsCount() >= msg.getEnemyPatronsListCount());
        data.setNoticeTime(System.currentTimeMillis() / 1000);
        data.setUserBaseInfo(PlayerBasePb.parseToUserBaseInfo(msg.getMyBaseInfo()));
        data.setEnemyBaseInfo(PlayerBasePb.parseToUserBaseInfo(msg.getEnemyBaseInfo()));
        data.setInsertOption();
        addNoticeToList(data);
    }

    public void addNoticeToList(CrossTradeWarNotice data) {

        synchronized (crossTradeWarNoticeMap) {
            if(crossTradeWarNoticeMap == null){
                crossTradeWarNoticeMap = new LinkedList<>();
            }
            if (crossTradeWarNoticeMap.size() >= GameConfig.CROSS_TRADE_WAR_MAX_NOTICE_NUMBER) {
                crossTradeWarNoticeMap.removeFirst();
            }
            crossTradeWarNoticeMap.addLast(data);
            if (data.getBattleType() == eTradeWarBattleType.Challenge.getValue()) {
                CrossTradeWarSimpleNotice simpleNotice = initCrossTradeWarSimpleNotice(data);


                String tag1 = "" + data.getUserBaseInfo().getServerId() + data.getUserId();
                if (!crossTradeWarSimpleNoticeMap.containsKey(tag1)) {
                    crossTradeWarSimpleNoticeMap.put(tag1, new ConcurrentHashMap<>());
                }
                Map<String, List<CrossTradeWarSimpleNotice>> userSimpleNotcieMap = crossTradeWarSimpleNoticeMap.get(tag1);
                String tag2 = "" + data.getEnemyBaseInfo().getServerId() + data.getEnemyUserId();
                if (!userSimpleNotcieMap.containsKey(tag2)) {
                    userSimpleNotcieMap.put(tag2, new ArrayList<>());
                }
                List<CrossTradeWarSimpleNotice> simpleNoticeList = userSimpleNotcieMap.get(tag2);
                simpleNoticeList.add(simpleNotice);
            }
            CrossTradeWarProto.CrossTradeWarNoticeListMsg.Builder listMsg = parseCrossTradeWarNoticeListMsg(crossTradeWarNoticeMap);
            crossTradeWarNoticeMsg = listMsg;
        }
        notifyServerNoticeList(data.getActivityId());
    }

    public CrossTradeWarProto.CrossTradeWarNoticeListMsg.Builder getNoticeListMsg() {
        if (null == crossTradeWarNoticeMsg) {
            return CrossTradeWarProto.CrossTradeWarNoticeListMsg.newBuilder();
        }
        return crossTradeWarNoticeMsg;
    }

    /**
     * 通知各个游戏服公告信息
     */
    private void notifyServerNoticeList(int activityId) {
        CrossTradeWarProto.CrossTradeWarNoticeListMsg.Builder listMsg = getNoticeListMsg();
        if (groupData != null){
            Set<Long> serverSet = groupData.getServerSet();
            CrossTradeWarProto.CrossTradeWarNoticeListSyncMsg.Builder syncMsg = CrossTradeWarProto.CrossTradeWarNoticeListSyncMsg.newBuilder();
            syncMsg.setActivityId(activityId);
            syncMsg.setDataList(listMsg);
            syncMsg.setGroupId(groupId);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_NOTICE_LIST_SYNC, syncMsg);
            for (long serverId : serverSet) {
                MessageHelper.sendPacket(serverId, 0, pbMsg);
            }
        }
    }

    /**
     * 获取游戏服公告信息
     */
    public void getServerNoticeList(int activityId, long serverId) {
        CrossTradeWarProto.CrossTradeWarNoticeListMsg.Builder listMsg = getNoticeListMsg();
        ActivityInfo activityInfo = CrossTradeWarActivityMgr.getActivityInfoIfNotLoadDb(activityId);
        if (null != activityInfo) {
            if (groupData != null){
                CrossTradeWarProto.CrossTradeWarNoticeListSyncMsg.Builder syncMsg = CrossTradeWarProto.CrossTradeWarNoticeListSyncMsg.newBuilder();
                syncMsg.setActivityId(activityId);
                syncMsg.setDataList(listMsg);
                syncMsg.setGroupId(groupId);
                YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_NOTICE_LIST_SYNC, syncMsg);
                MessageHelper.sendPacket(serverId, 0, pbMsg);
            }
        }
    }

    /**
     * 发布通缉
     *
     * @param activityId
     * @param userId
     * @param userBaseInfo
     * @param wantedUserId
     * @param wantedBaseInfo
     * @return
     */
    public int publishWanted(int activityId, long userId, UserBaseInfo userBaseInfo, long wantedUserId, UserBaseInfo wantedBaseInfo, int useItemId) {

        TradeWarWanted tradeWarWanted = initTradeWarWanted(activityId, userId, userBaseInfo, wantedUserId, wantedBaseInfo, 0, useItemId);
        synchronized (crossTradeWarWantedMap) {
            crossTradeWarWantedMap.put(tradeWarWanted.getWantedUid(), tradeWarWanted);
        }
        //增加通缉记录
        boolean isAddRecord = addWantedRecord(tradeWarWanted);
        //变更通缉榜单
        changeTradeWantedRank(tradeWarWanted);
        initTradeWarWantedMsg();
        //响应
        publishWantedResponse(tradeWarWanted, isAddRecord);
        //通知通缉
        notifyServerWanted(tradeWarWanted);
        return 0;
    }

    private static void publishWantedResponse(TradeWarWanted tradeWarWanted, boolean isAddRecord) {
        CrossTradeWarProto.CrossTradeWarPublishWantedToCrossRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarPublishWantedToCrossRespMsg.newBuilder();
        respMsg.setRet(0);
        respMsg.setActivityId(tradeWarWanted.getActivityId());
        respMsg.setWantedInfo(TradeWarPb.parseTradeWarWantedTempMsg(tradeWarWanted));
        respMsg.setUseItemId(tradeWarWanted.getUseItemId());
        respMsg.setIsAddWantedRecord(isAddRecord);
        YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_PUBLISH_WANTED_RESP, respMsg);
        MessageHelper.sendPacket(tradeWarWanted.getUserBaseInfo().getServerId(), tradeWarWanted.getUserId(), pbMsg);
    }

    /**
     * 通知游戏服通缉数据
     *
     * @param tradeWarWanted
     */
    private static void notifyServerWanted(TradeWarWanted tradeWarWanted) {
        ActivityInfo activityInfo = CrossTradeWarActivityMgr.getActivityInfo(tradeWarWanted.getActivityId());
        if (null != activityInfo) {
            TradeWarProto.TradeWarWantedTempMsg.Builder wantedMsg = TradeWarPb.parseTradeWarWantedTempMsg(tradeWarWanted);
            YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_WANTED_NOTIFY, wantedMsg);
            for (long serverId : activityInfo.getServerIdList()) {
                if (serverId != tradeWarWanted.getUserBaseInfo().getServerId()) {
                    MessageHelper.sendPacket(serverId, 0, pbMsg);
                }
            }
        }
    }

    /**
     * 初始化通缉msg
     *
     */
    private void initTradeWarWantedMsg() {
        long nowTime = System.currentTimeMillis() / 1000;
        if (crossTradeWarWantedMap == null){
            return;
        }
        if (crossTradeWarWantedMap.isEmpty()) {
            return;
        }
        synchronized (crossTradeWarWantedMap) {
            for (TradeWarWanted tradeWarWanted : crossTradeWarWantedMap.values()) {
                long publishUserId = tradeWarWanted.getUserId();
                if (tradeWarWanted.getFinishTime() > 0) {
                    crossTradeWarWantedMap.remove(tradeWarWanted.getWantedUid());
                    finishTradeWarWantedMap.addLast(tradeWarWanted);
                } else if (tradeWarWanted.getFinishTime() == 0 && (nowTime - tradeWarWanted.getWantedTime() > GameConfig.TRADE_WAR_WANTED_TIME
                        || isTrackFull(tradeWarWanted.getWantedUid(), publishUserId))) {
                    if (nowTime - tradeWarWanted.getWantedTime() > GameConfig.TRADE_WAR_WANTED_TIME) {
                        tradeWarWanted.setFinishTime(tradeWarWanted.getWantedTime());
                        crossTradeWarWantedMap.remove(tradeWarWanted.getWantedUid());
                        finishTradeWarWantedMap.addLast(tradeWarWanted);
                    } else if (isTrackFull(tradeWarWanted.getWantedUid(), publishUserId)) {
                        tradeWarWanted.setFinishTime(nowTime);
                        crossTradeWarWantedMap.remove(tradeWarWanted.getWantedUid());
                        finishTradeWarWantedMap.addLast(tradeWarWanted);
                    }
                }
            }
            finishTradeWarWantedMap.sort(tradeWarWantedSort);
            while (finishTradeWarWantedMap.size() > GameConfig.TRADE_WAR_MAX_FINISH_WANTED_COUNT) {
                finishTradeWarWantedMap.removeFirst();
            }
        }
        List<TradeWarWanted> tempWantedList = new ArrayList<>(finishTradeWarWantedMap);
        tempWantedList.addAll(crossTradeWarWantedMap.values());
        tradeWarWantedListMsg = TradeWarPb.parseTradeWarWantedListMsg(tempWantedList);
    }

    private TradeWarProto.TradeWarWantedListMsg.Builder getTradeWarWantedListMsg() {
        return tradeWarWantedListMsg;
    }

    /**
     * 获取通缉列表msg
     *
     * @param activityId
     * @param userId
     * @param serverId
     * @return
     */
    public TradeWarProto.TradeWarWantedListMsg.Builder getTradeWarWantedListMsg(int activityId, long userId, long serverId) {
        TradeWarProto.TradeWarWantedListMsg.Builder listMsg = TradeWarProto.TradeWarWantedListMsg.newBuilder();
        TradeWarProto.TradeWarWantedListMsg.Builder tempList = getTradeWarWantedListMsg();
        if (null != tempList) {
            for (TradeWarProto.TradeWarWantedTempMsg wantedTempMsg : tempList.clone().getWantedListList()) {
                TradeWarProto.TradeWarWantedTempMsg.Builder msg = wantedTempMsg.toBuilder();
                msg.setTrackTimes(getUserTraceTimes(msg.getWantedUid(), userId, serverId));
                listMsg.addWantedList(msg);
            }
        }
        return listMsg;
    }

    /**
     * 获取通缉详单
     *
     * @param activityId
     * @param wantedUid
     * @param userId
     * @param serverId
     * @return
     */
    public CrossTradeWarProto.CrossTradeWarWantedDetailRespMsg.Builder getTradeWarWantedDetailRespMsg(int activityId, String wantedUid, long userId, long serverId) {
        CrossTradeWarProto.CrossTradeWarWantedDetailRespMsg.Builder respMsg = CrossTradeWarProto.CrossTradeWarWantedDetailRespMsg.newBuilder();
        TradeWarWanted tradeWarWanted = getTradeWarWanted(wantedUid);
        if (null == tradeWarWanted) {
            tradeWarWanted = getFinishWanted(activityId, wantedUid);
            if (null == tradeWarWanted) {
                respMsg.setRet(GameErrorCode.E_TRADE_WAR_WANTED_NO_EXIST);
                return respMsg;
            }
        }
        TradeWarProto.TradeWarWantedTempMsg.Builder wantedMsg = TradeWarPb.parseTradeWarWantedTempMsg(tradeWarWanted);
        if (null != wantedMsg) {
            wantedMsg.setTrackTimes(getUserTraceTimes(wantedUid, userId, serverId));
            respMsg.setWantedInfo(wantedMsg);
        }
        List<TradeWarTrackRecord> recordList = getTradeWarTrackRecordList(wantedUid);
        if (null != recordList) {
            for (TradeWarTrackRecord trackRecord : recordList) {
                TradeWarProto.TradeWarTrackRecordTempMsg.Builder msg = TradeWarPb.parseTradeWarTrackRecordTempMsg(trackRecord);
                if (null != msg) {
                    respMsg.addTrackRecordList(msg);
                }
            }
        }
        respMsg.setRet(0);
        return respMsg;
    }

    /**
     * 判断能进行追缉
     *
     * @param activityId
     * @param wantedUid
     * @param userId
     * @param serverId
     * @return
     */
    public int canCrossTrack(int activityId, String wantedUid, long userId, long serverId, boolean calcTrackTimes,
                                    int dailyTrackTimes, int limitTrackTimes, String myUnionId) {
        TradeWarWanted tradeWarWanted = getTradeWarWanted(wantedUid);
        if (null == tradeWarWanted) {
            return GameErrorCode.E_TRADE_WAR_WANTED_NO_EXIST;
        }
        if (!isWantedTime(tradeWarWanted)) {
            return GameErrorCode.E_TRADE_WAR_WANTED_OVER_TIME;
        }
        long publishUserId = tradeWarWanted.getUserId();
        if (calcTrackTimes) {
            if (userId != publishUserId) {//不是自己发布的，判断一下次数是否够用
                int useItemId = tradeWarWanted.getUseItemId();
                GoodsInfo goodsInfo = GoodsMgr.getGoodsInfo(useItemId);
                int addTimes = 1;
                if (null != goodsInfo) {
                    addTimes = goodsInfo.getParamList().get(0).intValue();
                }
                if (dailyTrackTimes + addTimes > limitTrackTimes) {
                    return GameErrorCode.E_TRADE_WAR_TRACK_TIMES_OVER;
                }
            }
        }
        // 换掉 判断有多少人打过了 旧的是匹配次数 新版换成已被打的次数
        if (userId != publishUserId) {
            if (isHelpUserTrackFull(wantedUid, publishUserId)) {
                return GameErrorCode.E_TRADE_WAR_WANTED_OVER_BATTLE_TIMES;
            }
        } else {
            if (isPublishUserTrackFull(wantedUid, publishUserId)) {
                return GameErrorCode.E_TRADE_WAR_WANTED_OVER_BATTLE_TIMES;
            }
        }

        if (getUserTraceTimes(wantedUid, userId, serverId) >= GameConfig.TRADE_WAR_WANTED_USER_BATTLE_TIMES) {
            return GameErrorCode.E_TRADE_WAR_WANTED_TRACK_TIMES_OVER;
        }
        // 判断下发布者是否同商会
        UnionActivityGroupUnion unionData = Cross2UnionActivityGroupMgr.getUnionData(activityId, publishUserId);
        if (unionData != null) {
            // 发布者商会信息
            String unionUid = unionData.getUnionUid();
            if (StringUtils.isNullOrEmpty(unionUid) || unionUid.equals("0") || !unionUid.equals(myUnionId)) {
                // 判断下时间间隔
                if (System.currentTimeMillis() / 1000 - tradeWarWanted.getWantedTime() < GameConfig.TRADE_WAR_WANTED_LIMIT_TIME) {
                    return GameErrorCode.E_TRADE_WAR_TRACK_TOO_FAST_NO_SAME_UNION;
                }
            }
        }
        return 0;
    }

    /**
     * 追缉
     *
     * @param activityId
     * @param wantedUid
     * @param userId
     * @param serverId
     * @return
     */
    public int crossTrack(int activityId, String wantedUid, long userId, long serverId, String myUnionId) {
        int result = canCrossTrack(activityId, wantedUid, userId, serverId, false, 0, 0, myUnionId);
        if (0 != result) {
            return result;
        }
        TradeWarWanted tradeWarWanted = getTradeWarWanted(wantedUid);
        if (null != tradeWarWanted) {
            tradeWarWanted.setBattleTimes(tradeWarWanted.getBattleTimes() + 1);
        }
        initTradeWarWantedMsg();
        return 0;
    }

    /**
     * 获取完成的通缉列表
     *
     * @param activityId
     * @return
     */
    private LinkedList<TradeWarWanted> getFinishWantedList(int activityId) {
        return new LinkedList<>(finishTradeWarWantedMap);
    }

    /**
     * 获取完成的通缉
     *
     * @param activityId
     * @param wantedUid
     * @return
     */
    public TradeWarWanted getFinishWanted(int activityId, String wantedUid) {
        LinkedList<TradeWarWanted> dataList = getFinishWantedList(activityId);
        if (null != dataList) {
            for (TradeWarWanted data : dataList) {
                if (Objects.equals(data.getWantedUid(), wantedUid)) {
                    return data;
                }
            }
        }
        return null;
    }

    /**
     * 获取通缉map
     *
     * @return
     */
    public Map<String, TradeWarWanted> getTradeWarWantedMap() {
        if (crossTradeWarWantedMap != null) {
            return new ConcurrentHashMap<>(crossTradeWarWantedMap);
        }
        return null;
    }

    /**
     * 获取通缉
     *
     * @param wantedUid
     * @return
     */
    public TradeWarWanted getTradeWarWanted(String wantedUid) {
        Map<String, TradeWarWanted> dataMap = getTradeWarWantedMap();
        if (null != dataMap) {
            return dataMap.get(wantedUid);
        }
        return null;
    }

    /**
     * 获取追缉记录
     *
     * @param wantedUid
     * @return
     */
    public List<TradeWarTrackRecord> getTradeWarTrackRecordList(String wantedUid) {
        if (tradeWarTrackRecordMap.containsKey(wantedUid)) {
            return new ArrayList<>(tradeWarTrackRecordMap.get(wantedUid));
        }
        return null;
    }

    /**
     * 获取追缉次数
     *
     * @param wantedUid
     * @param userId
     * @param serverId
     * @return
     */
    public int getUserTraceTimes(String wantedUid, long userId, long serverId) {
        int times = 0;
        List<TradeWarTrackRecord> recordList = getTradeWarTrackRecordList(wantedUid);
        if (null != recordList) {
            for (TradeWarTrackRecord data : recordList) {
                if (data.getUserId() == userId && data.getUserBaseInfo().getServerId() == serverId) {
                    times++;
                }
            }
        }
        return times;
    }

    /**
     * 协助者的追击在此次通缉列表中是否满了(5.5新增需要发布用户id进行判断)
     *
     * @return
     */
    public boolean isHelpUserTrackFull(String wantedUid, long publishUserId) {
        return getTrackRecordCount(wantedUid) - getUserTrackRecordCount(wantedUid, publishUserId) >= GameConfig.TRADE_WAR_WANTED_MAX_BATTLE_TIMES;
    }

    /**
     * 发布者是否把自己发布通缉的追击次数打完了
     */
    public boolean isPublishUserTrackFull(String wantedUid, long publishUserId) {
        return getUserTrackRecordCount(wantedUid, publishUserId) >= GameConfig.TRADE_WAR_WANTED_USER_BATTLE_TIMES;
    }

    /**
     * 通缉是否满了
     *
     * @param wantedUid
     * @return
     */
    private boolean isTrackFull(String wantedUid) {
        return getTrackRecordCount(wantedUid) >= GameConfig.TRADE_WAR_WANTED_MAX_BATTLE_TIMES;
    }

    /**
     * v5.5 通缉是否满了
     *
     * @param wantedUid
     * @return
     */
    private boolean isTrackFull(String wantedUid, long publishUserId) {
        return isHelpUserTrackFull(wantedUid, publishUserId) && isPublishUserTrackFull(wantedUid, publishUserId);
    }

    /**
     * 获取被追击个数
     *
     * @param wantedUid
     * @return
     */
    private int getTrackRecordCount(String wantedUid) {
        int count = 0;
        List<TradeWarTrackRecord> dataList = getTradeWarTrackRecordList(wantedUid);
        if (null != dataList) {
            count = dataList.size();
        }
        return count;
    }

    /**
     * 获取一个用户在该通缉内的追击次数
     *
     * @param wantedUid
     * @param userId
     * @return
     */
    private int getUserTrackRecordCount(String wantedUid, long userId) {
        int count = 0;
        List<TradeWarTrackRecord> dataList = getTradeWarTrackRecordList(wantedUid);
        if (null != dataList) {
            for (TradeWarTrackRecord record : dataList) {
                if (record.getUserId() == userId) {
                    count++;
                }
            }
        }
        return count;

    }

    /**
     * 是否在通缉时间内
     *
     * @param wanted
     * @return
     */
    public boolean isWantedTime(TradeWarWanted wanted) {
        if (null == wanted) {
            return false;
        }
        long nowTime = System.currentTimeMillis() / 1000;
        return nowTime < wanted.getWantedTime() + GameConfig.TRADE_WAR_WANTED_TIME;
    }

    private static TradeWarWanted initTradeWarWanted(int activityId, long userId, UserBaseInfo userBaseInfo, long wantedUserId, UserBaseInfo wantedBaseInfo,
                                                     long wantedScore, int useItemId) {
        TradeWarWanted data = new TradeWarWanted();
        data.setActivityId(activityId);
        data.setWantedUid(UUIDHelper.randomUUID(String.valueOf(userBaseInfo.getServerId())));
        data.setUserId(userId);
        data.setUserBaseInfo(userBaseInfo);
        data.setWantedUserId(wantedUserId);
        data.setWantedBaseInfo(wantedBaseInfo);
        data.setWantedScore(wantedScore);
        data.setWantedTime(System.currentTimeMillis() / 1000);
        data.setBattleTimes(0);
        Property wantedReward = PropertyHelper.parseStringToProperty(GameConfig.TRADE_WAR_WANTED_REWARD);
        GoodsInfo wantedGoods = GoodsMgr.getGoodsInfo(useItemId);
        if (null != wantedGoods) {
            int param1 = wantedGoods.getParamList().get(0).intValue();
            wantedReward.rideProperty(param1);
        }
        data.setReward(PropertyHelper.parsePropertyToString(wantedReward));
        data.setScoreReward(false);
        data.setUseItemId(useItemId);
        data.setInsertOption();
        return data;
    }

    /**
     * 根据战斗结果增加追缉记录
     *
     * @param msg
     */
    public int addTrackRecordByResult(CrossTradeWarProto.CrossTradeWarToEnemyBattleRespMsg msg) {
        if (msg.getBattleType() != eTradeWarBattleType.Track.getValue()) {
            return 1;
        }
        TradeWarTrackRecord trackRecord = new TradeWarTrackRecord();
        trackRecord.setActivityId(msg.getActivityId());
        trackRecord.setRecordUid(UUIDHelper.randomUUID(String.valueOf(msg.getMyServerId())));
        trackRecord.setWantedUid(msg.getBattleUid());
        trackRecord.setUserId(msg.getMyUserId());
        trackRecord.setUserBaseInfo(PlayerBasePb.parseToUserBaseInfo(msg.getMyBaseInfo()));
        trackRecord.setKillPatronsCount(msg.getKillPatronsCount());
        trackRecord.setLoseScore(msg.getEnemyLoseScore());
        trackRecord.setRecordTime(System.currentTimeMillis() / 1000);
        trackRecord.setMyAddScore(msg.getMyAddScore());
        Property battleReward = PropertyHelper.parseStringToProperty(msg.getBattleReward());
        if (null != battleReward) {
            int tradeWarMoney = battleReward.getCountByGoodsId(GameConfig.GAME_MONEY_CROSS_TRADE_WAR_SCORE).intValue();
            trackRecord.setTradeWarMoney(tradeWarMoney);
        }
        trackRecord.setInsertOption();
        if (!tradeWarTrackRecordMap.containsKey(msg.getBattleUid())) {
            synchronized (tradeWarTrackRecordMap) {
                if (!tradeWarTrackRecordMap.containsKey(msg.getBattleUid())) {
                    tradeWarTrackRecordMap.put(msg.getBattleUid(), new ArrayList<>());
                }
            }
        }
        List<TradeWarTrackRecord> trackList = tradeWarTrackRecordMap.get(msg.getBattleUid());
        TradeWarWanted tradeWarWanted = getTradeWarWanted(msg.getBattleUid());
        if (null == tradeWarWanted) {
            return 1;
        }
        long publishUserId = tradeWarWanted.getUserId();
        boolean isTrackFull;
        synchronized (trackList) {
            if (publishUserId != msg.getMyUserId()) {
                isTrackFull = isHelpUserTrackFull(trackRecord.getWantedUid(), publishUserId);
            } else {
                isTrackFull = isPublishUserTrackFull(trackRecord.getWantedUid(), publishUserId);
            }
            if (isTrackFull) {//已经追缉满了
                return 1;
            }
            //判断是否追缉过了，避免重复追缉
            for (TradeWarTrackRecord tempData : trackList) {
                if (tempData.getUserId() == msg.getMyUserId()) {
                    return -1;
                }
            }
            trackList.add(trackRecord);
            isTrackFull = isTrackFull(trackRecord.getWantedUid(), publishUserId);
        }
        if (isTrackFull) {//追缉结束，通知发送邮件
            notifyWantedFinish(tradeWarWanted);
        }
        return 0;
    }

    /**
     * 通知通缉完成
     *
     * @param tradeWarWanted
     */
    private void notifyWantedFinish(TradeWarWanted tradeWarWanted) {
        synchronized (tradeWarWanted) {
            if (!tradeWarWanted.isScoreReward()) {
                CrossTradeWarProto.CrossTradeWarWantedFinishMsg.Builder notifyMsg = CrossTradeWarProto.CrossTradeWarWantedFinishMsg.newBuilder();
                notifyMsg.setWantedInfo(TradeWarPb.parseTradeWarWantedTempMsg(tradeWarWanted));
                List<TradeWarTrackRecord> trackRecordList = getTradeWarTrackRecordList(tradeWarWanted.getWantedUid());
                int trackCount = 0;
                int addScore = 0;
                int tradeWarMoney = 0;
                if (null != trackRecordList) {
                    trackCount = trackRecordList.size();
                    for (TradeWarTrackRecord trackRecord : trackRecordList) {
                        addScore += trackRecord.getMyAddScore();
                        tradeWarMoney += trackRecord.getTradeWarMoney();
                    }
                }
                Property reward = new Property(GameConfig.GAME_MONEY_CROSS_TRADE_WAR_SCORE, BigInteger.valueOf(tradeWarMoney));
                notifyMsg.setTrackCount(trackCount);
                notifyMsg.setAddScore(addScore);
                notifyMsg.setReward(PropertyHelper.parsePropertyToString(reward));
                YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.S_CROSS_TRADE_WAR_WANTED_FINISH, notifyMsg);
                MessageHelper.sendPacket(tradeWarWanted.getUserBaseInfo().getServerId(), tradeWarWanted.getUserId(), pbMsg);
                tradeWarWanted.setScoreReward(true);
            }
        }
    }

    public void wantedOverTimeTask() {
        List<Integer> needRemoveList = new ArrayList<>();

        if (!ActivityHelper.activityInShowTime(activityInfo)) {//活动不在了
            needRemoveList.add(activityInfo.getActivityId());
        }
        List<TradeWarWanted> dataList = new ArrayList<>(crossTradeWarWantedMap.values());
        for (TradeWarWanted wanted : dataList) {
            if (!isWantedTime(wanted) || isTrackFull(wanted.getWantedUid(), wanted.getUserId())) {
                notifyWantedFinish(wanted);
            }
        }

        for (Integer id : needRemoveList) {
            crossTradeWarWantedMap.remove(id);
        }
    }

    /**
     * 是否跨服追缉变更
     *
     * @param trackChangeType
     * @return
     */
    public boolean isCrossTrackChange(int trackChangeType) {
        return trackChangeType == eTradeWarBattleType.Challenge.getValue();
    }

    /**
     * 获取跨服简略公告
     *
     * @param userId
     * @param serverId
     * @param enemyUserId
     * @param enemyServerId
     * @return
     */
    public List<CrossTradeWarSimpleNotice> getCrossTradeWarSimpleNoticeList(long userId, long serverId, long enemyUserId, long enemyServerId) {

        if (null == crossTradeWarSimpleNoticeMap) {
            return null;
        }
        String tag1 = "" + serverId + userId;
        Map<String, List<CrossTradeWarSimpleNotice>> dataMap = crossTradeWarSimpleNoticeMap.get(tag1);
        if (null == dataMap) {
            return null;
        }
        String tag2 = "" + enemyServerId + enemyUserId;
        List<CrossTradeWarSimpleNotice> dataList = dataMap.get(tag2);
        if (null != dataList) {
            return new ArrayList<>(dataList);
        }
        return null;
    }

    /**
     * 获取商战通缉榜榜首
     *
     * @return
     */
    public RankProto.RankTempMsg.Builder getTradeWarWantedRankTop() {

        if (null != wantedRankMap) {
            return wantedRankMap.getTopRankMsg();
        } else {
            return null;
        }
    }

    /**
     * 获取商战通缉榜

     * @return
     */
    public RankProto.RankListMsg.Builder getTradeWarWantedRankMsg() {
        if (null != wantedRankMap) {
            return wantedRankMap.getRankListMsg();
        } else {
            return RankProto.RankListMsg.newBuilder();
        }
    }

    /**
     * 初始化公告
     */
    private void initTradeWarNoticeList(LinkedList<CrossTradeWarNotice> noticeListFromDb) {
        long zeroTime = LocalDateTimeHelper.getZeroTimeTimeStamp();
        LinkedList<CrossTradeWarNotice> resultNoticeMap = new LinkedList<>();
        Map<String, Map<String, List<CrossTradeWarSimpleNotice>>> tempSimpleNoticeMap = new ConcurrentHashMap<>();

        for (CrossTradeWarNotice tradeWarNotice : noticeListFromDb) {
            if (Cross2UnionActivityGroupMgr.getGroupId(activityInfo.getActivityId(), tradeWarNotice.getUserId()) != groupId) {
                continue;
            }
            if (tradeWarNotice.getBattleType() == eTradeWarBattleType.Challenge.getValue()) {
                CrossTradeWarSimpleNotice simpleNotice = initCrossTradeWarSimpleNotice(tradeWarNotice);

                String tag1 = "" + simpleNotice.getServerId() + simpleNotice.getUserId();
                if (!tempSimpleNoticeMap.containsKey(tag1)) {
                    tempSimpleNoticeMap.put(tag1, new ConcurrentHashMap<>());
                }
                Map<String, List<CrossTradeWarSimpleNotice>> tempMap = tempSimpleNoticeMap.get(tag1);
                String tag2 = "" + simpleNotice.getEnemyServerId() + simpleNotice.getEnemyUserId();
                if (!tempMap.containsKey(tag2)) {
                    tempMap.put(tag2, new ArrayList<>());
                }
                List<CrossTradeWarSimpleNotice> tempList = tempMap.get(tag2);
                tempList.add(simpleNotice);
            }

            if (resultNoticeMap.size() >= GameConfig.CROSS_TRADE_WAR_MAX_NOTICE_NUMBER) {
                resultNoticeMap.remove(0);
            }
            resultNoticeMap.add(tradeWarNotice);
        }

        crossTradeWarNoticeMap = resultNoticeMap;
        crossTradeWarSimpleNoticeMap = tempSimpleNoticeMap;
        lastNoticeResetTime = zeroTime;
    }

    /**
     * 初始化简略公告实体
     *
     * @param tradeWarNotice
     * @return
     */
    private static CrossTradeWarSimpleNotice initCrossTradeWarSimpleNotice(CrossTradeWarNotice tradeWarNotice) {
        CrossTradeWarSimpleNotice data = new CrossTradeWarSimpleNotice();
        data.setUserId(tradeWarNotice.getUserId());
        data.setServerId(tradeWarNotice.getUserBaseInfo().getServerId());
        data.setEnemyUserId(tradeWarNotice.getEnemyUserId());
        data.setEnemyServerId(tradeWarNotice.getEnemyBaseInfo().getServerId());
        data.setNoticeTime(tradeWarNotice.getNoticeTime());
        data.setKillPatronsCount(tradeWarNotice.getKillPatronsCount());
        return data;
    }

    /**
     * 计算公告战斗数量
     *
     * @param tradeWarNotice
     */
    private void calcTradeWarNoticeChallengeTimes(CrossTradeWarNotice tradeWarNotice) {
        int totalChallengeTimes = 0;
        int totalKillPatronsCount = 0;
        List<CrossTradeWarSimpleNotice> tempList = getCrossTradeWarSimpleNoticeList(tradeWarNotice.getUserId(),
                tradeWarNotice.getUserBaseInfo().getServerId(), tradeWarNotice.getEnemyUserId(), tradeWarNotice.getEnemyBaseInfo().getServerId());
        if (null != tempList) {
            totalChallengeTimes = tempList.size();
            for (CrossTradeWarSimpleNotice simpleNotice : tempList) {
                totalKillPatronsCount += simpleNotice.getKillPatronsCount();
            }
        }
        tradeWarNotice.setTotalChallengeTimes(totalChallengeTimes);
        tradeWarNotice.setTotalKillPatronsCount(totalKillPatronsCount);
    }

    /**
     * 初始化msg
     *
     * @param crossTradeWarNoticeList
     * @return
     */
    private CrossTradeWarProto.CrossTradeWarNoticeListMsg.Builder parseCrossTradeWarNoticeListMsg(LinkedList<CrossTradeWarNotice> crossTradeWarNoticeList) {
        List<CrossTradeWarNotice> tempList;
        synchronized (crossTradeWarNoticeList) {
            tempList = new ArrayList<>(crossTradeWarNoticeList);
        }
        LinkedList<CrossTradeWarNotice> resultList = new LinkedList<>();
        for (int i = tempList.size() - 1; i >= 0; i--) {
            CrossTradeWarNotice data = tempList.get(i);
            boolean isFind = false;
            if (data.getBattleType() == eTradeWarBattleType.Challenge.getValue()) {
                for (CrossTradeWarNotice loopData : resultList) {
                    if (loopData.getBattleType() == eTradeWarBattleType.Challenge.getValue() &&
                            data.getUserId() == loopData.getUserId() && data.getUserBaseInfo().getServerId() == loopData.getUserBaseInfo().getServerId() &&
                            data.getEnemyUserId() == loopData.getEnemyUserId() && data.getEnemyBaseInfo().getServerId() == loopData.getEnemyBaseInfo().getServerId()) {
                        isFind = true;
                        break;
                    }
                }
            }
            if (!isFind) {
                calcTradeWarNoticeChallengeTimes(data);
                resultList.addFirst(data);
            }
        }
        return CrossTradeWarPb.parseCrossTradeWarNoticeListMsg(resultList);
    }

    /**
     * 初始化通缉榜
     */
    private CrossTradeWarWantedRankListModel initWantedRank() {
        CrossTradeWarWantedRankListModel rankListModel = wantedRankMap;
        if (wantedRankMap == null) {
            rankListModel = initWantedRankListModel(activityInfo.getActivityId(), crossTradeWarWantedMap);
        }
        return rankListModel;
    }

    /**
     * 初始化通缉榜
     *
     * @param activityId
     * @param dataMap
     * @return
     */
    private static CrossTradeWarWantedRankListModel initWantedRankListModel(int activityId, Map<String, TradeWarWanted> dataMap) {
        CrossTradeWarWantedRankListModel rankListModel = new CrossTradeWarWantedRankListModel(activityId);
        LinkedList<CrossUserRank> userRankList = new LinkedList<>();
        for (TradeWarWanted wanted : dataMap.values()) {
            CrossUserRank userRank = null;
            for (CrossUserRank data : userRankList) {
                if (data.getUserId() == wanted.getUserId() && data.getUserBaseInfo().getServerId() == wanted.getUserBaseInfo().getServerId()) {
                    userRank = data;
                    break;
                }
            }
            if (null == userRank) {
                userRank = new CrossUserRank();
                userRank.setUserId(wanted.getUserId());
                userRank.setValue(BigInteger.ZERO);
                userRankList.addLast(userRank);
            }
            if (wanted.getWantedTime() > userRank.getLastUpdateTime()) {
                userRank.setLastUpdateTime(wanted.getWantedTime());
                userRank.setUserBaseInfo(wanted.getUserBaseInfo());
            }
            BigInteger addValue;
            if (0 == wanted.getUseItemId() || GameConfig.TRADE_WAR_WANTED_ITEM_ID == wanted.getUseItemId()) {
                addValue = BigInteger.ONE;
            } else {
                addValue = GoodsMgr.getGoodsComposeCount(wanted.getUseItemId(), GameConfig.TRADE_WAR_WANTED_ITEM_ID);
            }
            userRank.setValue(userRank.getValue().add(addValue));
        }
        rankListModel.setRankList(userRankList);
        return rankListModel;
    }

    /**
     * 初始化通缉记录
     */
    private Map<String, Map<String, CrossTradeWarWantedRecord>> initWantedRecordMap() {
        Map<String, Map<String, CrossTradeWarWantedRecord>> resultMap = new ConcurrentHashMap<>();
        for (Map.Entry <String, TradeWarWanted> dataEntry : crossTradeWarWantedMap.entrySet()) {
            TradeWarWanted data = dataEntry.getValue();
            String tag1 = "" + data.getUserBaseInfo().getServerId() + data.getUserId();
            if (!resultMap.containsKey(tag1)) {
                resultMap.put(tag1, new ConcurrentHashMap<>());
            }
            Map<String, CrossTradeWarWantedRecord> userMap = resultMap.get(tag1);
            String tag2 = "" + data.getWantedBaseInfo().getServerId() + data.getWantedUserId();
            if (!userMap.containsKey(tag2)) {
                CrossTradeWarWantedRecord wantedRecord = initCrossTradeWarWantedRecord(data);
                userMap.put(tag2, wantedRecord);
            }

        }
        return resultMap;
    }

    /**
     * 增加商战记录
     *
     * @param tradeWarWanted
     * @return 是否新增
     */
    private boolean addWantedRecord(TradeWarWanted tradeWarWanted) {
        if (wantedRecordMap == null){
            wantedRecordMap = new ConcurrentHashMap<>();
        }
        String tag1 = "" + tradeWarWanted.getUserBaseInfo().getServerId() + tradeWarWanted.getUserId();
        if (!wantedRecordMap.containsKey(tag1)) {
            synchronized (wantedRecordMap) {
                if (!wantedRecordMap.containsKey(tag1)) {
                    wantedRecordMap.put(tag1, new ConcurrentHashMap<>());
                }
            }
        }
        Map<String, CrossTradeWarWantedRecord> userMap = wantedRecordMap.get(tag1);
        String tag2 = "" + tradeWarWanted.getWantedBaseInfo().getServerId() + tradeWarWanted.getWantedUserId();
        if (!userMap.containsKey(tag2)) {
            synchronized (userMap) {
                if (!userMap.containsKey(tag2)) {
                    CrossTradeWarWantedRecord wantedRecord = initCrossTradeWarWantedRecord(tradeWarWanted);
                    userMap.put(tag2, wantedRecord);
                    return true;
                }
            }
        }

        synchronized (userMap) {
            // 如果已经有记录，将时间更靠后的放入，取到最新的通缉记录用于再次通缉
            CrossTradeWarWantedRecord preWantedRecord = userMap.get(tag2);
            if (preWantedRecord != null && tradeWarWanted.getWantedTime() > preWantedRecord.getWantedTime()) {
                CrossTradeWarWantedRecord wantedRecord = initCrossTradeWarWantedRecord(tradeWarWanted);
                userMap.put(tag2, wantedRecord);
            }
        }

        return false;
    }

    /**
     * 初始化通缉记录
     *
     * @param tradeWarWanted
     * @return
     */
    private static CrossTradeWarWantedRecord initCrossTradeWarWantedRecord(TradeWarWanted tradeWarWanted) {
        CrossTradeWarWantedRecord data = new CrossTradeWarWantedRecord();
        data.setActivityId(tradeWarWanted.getActivityId());
        data.setUserId(tradeWarWanted.getUserId());
        data.setServerId(tradeWarWanted.getUserBaseInfo().getServerId());
        data.setWantedUserId(tradeWarWanted.getWantedUserId());
        data.setWantedServerId(tradeWarWanted.getWantedBaseInfo().getServerId());
        data.setWantedTime(tradeWarWanted.getWantedTime());
        return data;
    }

    /**
     * 获取通缉记录列表
     *
     * @param activityId
     * @param userId
     * @param serverId
     * @return
     */
    public List<CrossTradeWarWantedRecord> getTradeWarWantedRecordList(int activityId, long userId, long serverId) {

        if (null == wantedRecordMap) {
            return null;
        }
        String tag1 = "" + serverId + userId;
        Map<String, CrossTradeWarWantedRecord> userMap = wantedRecordMap.get(tag1);
        if (null == userMap) {
            return null;
        }
        return new ArrayList<>(userMap.values());
    }

    /**
     * 变更通缉榜
     *
     * @param tradeWarWanted
     */
    private void changeTradeWantedRank(TradeWarWanted tradeWarWanted) {
        if (wantedRankMap == null){
            wantedRankMap = initWantedRankListModel(tradeWarWanted.getActivityId(), new ConcurrentHashMap<>());
        }

        if (null != wantedRankMap) {
            BigInteger addValue;
            if (0 == tradeWarWanted.getUseItemId() || GameConfig.TRADE_WAR_WANTED_ITEM_ID == tradeWarWanted.getUseItemId()) {
                addValue = BigInteger.ONE;
            } else {
                addValue = GoodsMgr.getGoodsComposeCount(tradeWarWanted.getUseItemId(), GameConfig.TRADE_WAR_WANTED_ITEM_ID);
            }
            wantedRankMap.rankChange(tradeWarWanted, addValue);
        }

    }

    /**
     * 描述：是否重复追缉
     * 作者：zrq
     * 时间：2023/2/14
     * 参数：
     * <p>
     * 返回值：
     **/
    public boolean isRepeatTrack(String wantedUid, long userId) {
        List<TradeWarTrackRecord> trackList = tradeWarTrackRecordMap.get(wantedUid);
        if (null == trackList) {
            return false;
        }
        synchronized (trackList) {
            //判断是否追缉过了，避免重复追缉
            for (TradeWarTrackRecord tempData : trackList) {
                if (tempData.getUserId() == userId) {
                    return true;
                }
            }
        }
        return false;
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        this.groupId = groupId;
    }
}
